package com.abe.signaturesdk;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.abe.signaturesdk.views.SignaturePad;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;


public class SignatureDialog extends Dialog {

    private Context mContext;
    private Activity activity;
    private SignaturePad mSignaturePad;
    private Button mClearButton;
    private Button mSaveButton;
    private String imgName = "";
    private Boolean qmhp = false;
    private Boolean isLanFull = true;
    private int dialogWidth = LayoutParams.MATCH_PARENT;
    private int dialogHeight = LayoutParams.MATCH_PARENT;
    private OnSignatureCallBack callBack;

    private int layoutTheme = ThemeNormal;
    public static final int ThemeNormal = 0x001;
    public static final int ThemeBlueBlack = 0x002;

    @IntDef({ThemeNormal, ThemeBlueBlack})
    @Retention(RetentionPolicy.SOURCE)
    public @interface ThemeType {
    }

    public SignatureDialog(Context context) {
        super(context);
        this.mContext = context;
        this.imgName = String.valueOf(System.currentTimeMillis());
        if (context instanceof Activity) {
            activity = (Activity) context;
        }
        setOwnerActivity(activity);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    public SignatureDialog(Context context, String imgName) {
        this(context);
        this.imgName = imgName;
    }

    public SignatureDialog(Context context, @ThemeType int layoutTheme) {
        this(context);
        this.layoutTheme = layoutTheme;
    }

    public SignatureDialog(Context context, String imgName, @ThemeType int layoutTheme) {
        this(context, imgName);
        this.layoutTheme = layoutTheme;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        switch (layoutTheme) {
            case ThemeBlueBlack:
                setContentView(R.layout.electronic_signature_theme_blue_black);
                LinearLayout item_switch_pm = findViewById(R.id.item_switch_pm);
                item_switch_pm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        boolean tempQmhp = !qmhp;
                        if (isLanFull) {
                            if (tempQmhp) {
                                setDialogWindowAttr(true);
                            } else {
                                setDialogWindowAttr(dialogWidth, dialogHeight);
                            }
                        } else {
                            setDialogWindowAttr(dialogWidth, dialogHeight, tempQmhp);
                        }
                        qmhp = tempQmhp;
                    }
                });
                break;
            default:
                setContentView(R.layout.electronic_signature);
                break;
        }
        mSignaturePad = findViewById(R.id.signature_pad);
        mClearButton = findViewById(R.id.clear_btn);
        mSaveButton = findViewById(R.id.positiveButton);
        Button negativeButton = findViewById(R.id.negativeButton);
        mSignaturePad.setOnSignedListener(new SignaturePad.OnSignedListener() {
            @Override
            public void onStartSigning() {
            }

            @Override
            public void onSigned() {
                mSaveButton.setEnabled(true);
                mClearButton.setEnabled(true);
            }

            @Override
            public void onClear() {
                mSaveButton.setEnabled(false);
                mClearButton.setEnabled(false);
            }
        });

        mClearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSignaturePad.clear();
            }
        });

        mSaveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bitmap signatureBitmap = mSignaturePad.getSignatureBitmap();
                if (callBack != null) {
                    if (CommonTool.addJpgSignatureToGallery(mContext, signatureBitmap, imgName)) {
                        callBack.onSignEnd(signatureBitmap, CommonTool.getImgPath(imgName));
                    }
                }
                dismiss();
            }
        });
        negativeButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setCallBackListener(
            OnSignatureCallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public void dismiss() {
        CommonTool.portrait(activity);
        super.dismiss();
    }

    //*************************************************************************


    //默认设置横屏模式MATCH_PARENT
    private void setDialogWindowAttrWithLanFull(float width, float height, Boolean qmhp) {
        isLanFull = true;
        this.qmhp = qmhp;
        if (qmhp) {
            CommonTool.landscape(activity);
        } else {
            this.dialogWidth = (int) width;
            this.dialogHeight = (int) height;
            CommonTool.portrait(activity);
        }
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.CENTER;
            lp.width = (int) width;
            lp.height = (int) height;
            window.setAttributes(lp);
        }
    }

    //下部分为调整签名Dialog的方向
    private void setDialogWindowAttr(Boolean qmhp) {
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.CENTER;
            lp.width = LayoutParams.MATCH_PARENT;// 宽高可设置具体大小
            if (qmhp) {
                lp.width = LayoutParams.MATCH_PARENT;
                lp.height = LayoutParams.MATCH_PARENT;
            } else {
                lp.width = dialogWidth;
                lp.height = dialogHeight;
            }
            setDialogWindowAttrWithLanFull(lp.width, lp.height, qmhp);
        }
    }

    public void setDialogWindowAttr(float width, float height) {
        setDialogWindowAttrWithLanFull(width, height, false);
    }

    public void setDialogWindowAttr(float width, float height, Boolean qmhp) {
        this.qmhp = qmhp;
        isLanFull = false;
        this.dialogWidth = (int) width;
        this.dialogHeight = (int) height;
        if (qmhp) {
            CommonTool.landscape(activity);
        } else {
            CommonTool.portrait(activity);
        }
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.CENTER;
            lp.width = (int) width;
            lp.height = (int) height;
            window.setAttributes(lp);
        }
    }

    public void setDialogWindowDefault(Boolean qmhp) {
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.CENTER;
            lp.width = LayoutParams.MATCH_PARENT;// 宽高可设置具体大小
            this.dialogWidth = lp.width;
            WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
            int height = wm.getDefaultDisplay().getHeight();
            if (height <= CommonTool.Dp2Px(mContext, 500)) {
                lp.height = LayoutParams.MATCH_PARENT;
            } else {
                lp.height = CommonTool.Dp2Px(mContext, 500);
            }
            this.dialogHeight = lp.height;
            if (qmhp) {
                lp.height = LayoutParams.MATCH_PARENT;
            }
            setDialogWindowAttrWithLanFull(lp.width, lp.height, qmhp);
        }
    }
}